using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;

namespace GameStateManagementSample.QuickMath
{
    class SliderMath
    {
        Texture2D m_Texture;
        SpriteFont m_Font;

        public MyOperator[] m_Operator;

        public float m_PosY,m_PosYold; // xac dinh vi tri cua tung o vuong
        
        Rectangle m_Rect; // de tinh toan va cham khi truot

        int offset = 230; // dich chuyen 1 doan so voi ban dau
        private bool isSlider; // kiem tra co phai dang touch va di chuyen
        private int idTouch; // xac dinh touch nao cham truoc dung trong multitouch
        private Vector2 posTouchOld; // luu lai vi tri touch dau tien


        public SliderMath(int _X, Texture2D _tex, SpriteFont _font)
        {
            m_Texture = _tex;
            m_Font = _font;
            m_Rect = new Rectangle(_X, 0, 120, 480);
            m_PosY = 0;

            m_Operator = new MyOperator[8];
            for (int i = 0; i < 8;i++ )
            {
                m_Operator[i] = new MyOperator();
                /*m_Operator[i].m_First = MyOperator.type.Number;
                m_Operator[i].m_Sec = i;*/
            }
        }
        
        public void InputTouch(TouchCollection touchs)
        {
            foreach (TouchLocation touch in touchs)
            {
                if (touch.State == TouchLocationState.Pressed && isSlider == false)
                {
                    if (m_Rect.Contains((int)touch.Position.X, (int)touch.Position.Y))
                    {
                        isSlider = true;
                        idTouch = touch.Id;
                        posTouchOld = touch.Position;
                        m_PosYold = m_PosY;
                    }
                }

                if (touch.State == TouchLocationState.Moved)
                {
                    if (touch.Id == idTouch && isSlider == true)
                    {
                        if (m_Rect.Contains((int)touch.Position.X, (int)touch.Position.Y))
                        {
                            m_PosY =m_PosYold+ touch.Position.Y - posTouchOld.Y;
                            if (m_PosY >= 800) m_PosY = 0;
                            if (m_PosY < 0) m_PosY = 800;
                        }
                        else
                        {
                            isSlider = false;
                        }
                    }
                }
                if (touch.State == TouchLocationState.Released && touch.Id == idTouch)
                {
                    isSlider = false;
                }
            }
        }
        public void Update(GameTime gameTime)
        {
            if (isSlider == false)
            {
                int temp = ((int)m_PosY + 50) / 100;
                temp *= 100;
                if (m_PosY < temp )
                {
                    m_PosY += 60 * (float)gameTime.ElapsedGameTime.TotalSeconds;
                    if (m_PosY > temp) m_PosY = temp;                   
                }
                if (m_PosY > temp)
                {
                    m_PosY -= 60 * (float)gameTime.ElapsedGameTime.TotalSeconds;
                    if (m_PosY < temp) m_PosY = temp;
                }
            }
        }
        public void Draw(SpriteBatch _spriteBatch)
        {
            for (int i = 0; i < 8;i++ )
            {
                float temp = i * 100 + m_PosY + offset;
                while(temp > 640)temp -= 800;
                _spriteBatch.Draw(m_Texture, new Vector2(m_Rect.X,temp ), Color.White);
                _spriteBatch.DrawString(m_Font, m_Operator[i].ToString(), new Vector2(m_Rect.X + 25, temp+20), Color.Black);
            }
        }

        public MyOperator OperatorSelect()
        {
            int select = (int)((m_PosY + 50) / 100); 
            
            select = 8 - select;
            if (select == 8) select = 0;
            return m_Operator[select];
        }
    }
}
